//
//  SWProjectListView.m
//  YiYueHairdresser
//
//  Created by Sky on 2018/10/30.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "SWProjectListView.h"
#import "AppDelegate.h"

@interface SWProjectListView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SWProjectListView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

#pragma mark - 初始化
- (void)initUI {
    
    [self addSubview:self.tableView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self removeFromSuperview];
}

#pragma mark - 公共
- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    [self.tableView reloadData];
}

- (void)setListFrame:(CGRect)listFrame{
    self.tableView.frame = listFrame;
}

- (void)showView {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self];
}

#pragma mark - tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return self.titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * addView = [[UIView alloc]init];
    return addView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [UIView new];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *identifier = @"SWProjectListCEll";
    
       UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            UILabel *title = [UILabel new];
            title.font = [UIFont systemFontOfSize:14];
            title.textColor = UIColor.blackColor;
            title.textAlignment = NSTextAlignmentCenter;
            title.tag = 1001;
            [cell.contentView addSubview:title];
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.inset(0);
            }];
        }
    UILabel *titleLab = [cell.contentView viewWithTag:1001];
    STServiceCategoryModel * model = self.titleArray[indexPath.row];
    titleLab.text =model.name;
    return cell;
   
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.SelectBackBlock((int)indexPath.row);
    [self removeFromSuperview];
}

#pragma mark - 懒加载
-(UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.groupTableViewBackgroundColor;
        //1.调整(iOS7以上)表格分隔线边距
        if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            _tableView.separatorInset = UIEdgeInsetsZero;
        }
        //2.调整(iOS8以上)view边距(或者在cell中设置preservesSuperviewLayoutMargins,二者等效)
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            _tableView.layoutMargins = UIEdgeInsetsZero;
        }
    }
    return _tableView;
}

@end
